<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<!DOCTYPE html>
<html>

<head lang="en">
<%@ include file="./commoms/link.txt"%>

<meta name="description" content="register">
<style type="text/css">
.header {
	text-align: center;
}

.header h1 {
	font-size: 200%;
	color: #333;
	margin-top: 30px;
}

.header p {
	font-size: 14px;
}

.get {
	text-align: center;
	padding: 100px 0;
}
#inputCode {
	display: inline-block;
	height: 40px;
	vertical-align: middle;
	border: none;
	border-radius: 10px;
	border-bottom: #DDDDDD solid 2px;
	padding-left: 10px;
}

.mycode {
	display: inline-block;
	width: 80px;
	height: 40px;
	vertical-align: middle;
}
</style>
</head>

<body>
	<header class="am-topbar am-topbar-fixed-top">
		<div class="am-container">
			<h1 class="am-topbar-brand">
				<a href="index.html">儿童位置跟踪系统</a>
			</h1>
		</div>
	</header>
	<div class="get">
		<div class="am-g">
			<div class="am-u-lg-3  am-u-lg-offset-7 am-padding-vertical-sm"
				style="border-style: solid; border-width: medium">
				<ul class="am-nav am-nav-pills am-nav-justify">
					<li><a href="./Login.jsp">用户登录</a></li>
					<li class="am-active"><a href="./Register.jsp">免费注册</a></li>
				</ul>
				<hr>
				<form method="post" action="#" class="am-form"
					id="registerformtooltip" data-am-validator>
					<div class="am-form-group">
						<input type="text" name="PhoneNumber" id="PhoneNumber"
							 maxlength="11" class="am-form-field"
							placeholder="请使用手机号码注册" pattern="1[0-9]{10}"
							data-foolish-msg="请输入正确的手机号码" required />
					</div>
					<div class="am-g am-form-group">
                            <div  class="am-u-lg-7">
							<input type="text" name="validateCode"
								class="am-form-field" pattern="[0-9]{4}" 
								maxlength="4" data-foolish-msg="请输入正确验证码" placeholder="填写验证码"
								required />
						</div>
						<div  class="am-u-lg-5 am-u-end" >
							<Button type="button" id="verification-code" 
									name="verification-code" 
									class="am-radius am-btn-warning am-btn am-btn-block"><span >获取验证码 </span></Button>
</div>
					</div>
					<div class="am-form-group">
						<input type="password" name="Password" id="Password"
							class="am-form-field" placeholder="请设置登陆密码" 
							maxlength="20" pattern="[a-z|A-Z|0-9]{6,20}"
							data-foolish-msg="请输入密码数字加字母" required />
					</div>
					<div class="am-form-group">
						<input type="password" name="PasswordRepeat" class="am-form-field"
							data-equal-to="#Password" placeholder="请重复密码"
							data-foolish-msg="请与上面保存一致" required id="rePassword" />
					</div>
					
					<div class="am-form-group">
						<input type="button" name="submit" id="submit" value="立即注册"
							class="am-btn am-btn-primary am-btn-sm am-btn-block">
					</div>
				</form>
			</div>
			 	<div id="codediv" class='am-hide'>
				<input id="inputCode" type="text" /> <span id="code" class="mycode"></span>
			</div>
		</div>
		  
	</div>
	<script type="text/javascript">
		var $form = $('#registerformtooltip');
		var i = 60;
		var inp = document.getElementById('inputCode');
		var code = document.getElementById('code');
		function showTime() {
			i = i - 1;
			$('#verification-code').text(i + 's重新发送');
		}
	    var times=0;
	    var isvalidate=true;
		var codemodal=myHint('验证码', $('#codediv'));
		var c = new KinerCode({
			len : 4,//需要产生的验证码长度
			chars : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c', 'd', 'e',
					'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
					'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
					'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
					'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ],
			question : false,
			copy : true,
			randomBg : true,
			inputArea : inp,
			codeArea : code,
			click2refresh : true,
			false2refresh : true,
			validateEven : "blur",
			validateFn : function(result, code) {
				if (result) {
					isvalidate=true;
					codemodal.modal('close');
					$('#codediv').attr('class','am-hide');
				} else {

					if (this.opt.question) {
					} else {
					}
				}
			}
		});
		setFormValidate($form);
		$('#submit').click(function(){
		
				submit();
		});
				function submit(){
					if(isvalidate==false){
						$('#codediv').attr('class','');
						codemodal.modal('open');
						c.refresh();
						return false;
						}
					var validatecode = $('[name=validateCode]').val();
					var PhoneNumber = $("#PhoneNumber").val();
					var Password = $("#Password").val();
					var rePassword = $("#rePassword").val();
					var regvalidateCode = /[0-9]{4}/;
					var regPhoneNumber = /1[0-9]{10}/;
					var regPassword = /[a-z|A-Z|0-9]{6,20}/;
					if (!regvalidateCode.test(validatecode)
							|| !regPhoneNumber.test(PhoneNumber)
				             ||!regPassword.test(Password)
							||!regPassword.test(rePassword)
							|| rePassword != Password) {
						$form.submit();
						return false;
					} else {
						myAjax(domain + "/api/user/register.do", {
							"validatecode" : validatecode,
							"phonenumber" : PhoneNumber,
							"password" : Password,
							  "web":1
						}, function(data) {
							if (data.code == '200') {
								 window.location.href = "userinfo.jsp";
							} else {
								times++;
								if(data.code='406')
									{
								myAlert('提示','验证码错误');
								$('[name=validateCode]').focus();
									}
								if(data.code='403')
								{
							myAlert('提示','手机号已被注册');
							$("#PhoneNumber").focus();
								}
								if(times>=3)
								{
									$('#codediv').attr('class','');
									isvalidate=false;
							
									codemodal.modal('open');
									c.refresh();
										
								}

							}
						});
					}
				}
			

		$('#verification-code').click(
				function() {
					if(isvalidate==false){
						$('#codediv').attr('class','');
						codemodal.modal('open');
						c.refresh();
						return false;
						}
					var $btn = $(this)
					var text = $("#PhoneNumber").val();
					var reg = /1[0-9]{10}/;
					var registermessage = getCookie('registermessage');
					if (registermessage == null || registermessage != text) {

						if (reg.test(text)) {
							
							myAjax(domain + "/api/user/getValidateCode.do", {
								"phonenumber" : text,
							}, function(data) {
								if(data.code == '406'){
									
									myAlert('提示', '手机号码不正确');
								}else
								if (data.code == '403') {
                                
									myAlert('提示', '该用户已被注册');
								
									times++;
									$("#PhoneNumber").focus();
									isvalidate=false;

								} else if (data.code == '200'){
									var mins = 5;
									var int = setInterval(showTime, 1000);
									var exp = new Date();
									exp.setTime(exp.getTime() + mins * 1000
											* 60);
									document.cookie = "registermessage=" + text
											+ ";expires=" + exp.toGMTString();
									$btn.button('loading');
									setTimeout(function() {
										clearInterval(int);
										i = 60;
										$btn.button('reset');
									}, 60000);
								}

							});
						} else {
							$("#PhoneNumber").focus();
						}
					} else
						myAlert('提示','请稍后再试!!!');
				}

		);
		
	</script>
	
</body>


</html>
